package com.firesys.httpserver;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;


import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;


/**
 * 批量下载图片管理
 * @author king
 *
 */
@SuppressLint("NewApi")
public class DownloadManager {
        // 下载地址
 	private String url;
 	// 下载任务线程池
 	private ExecutorService executorService;
 	// 应用程序上下文
 	private Context context;
 	
 	private Handler downhandler;
 	
       private  SQLiteDatabase db;
       
       private RequestUrl r;
 	
 	public DownloadManager(Context context,Handler downhandler,SQLiteDatabase db) {
		super();
		this.context = context;
		this.downhandler=downhandler;
		this.db=db;
		r=new RequestUrl(context);
		
	}
 	
 	public void addTask( DownloadItem item) {
		
		executorService.submit(new DownloadThread(item));
	}

	public void stop() {
		executorService.shutdown();
	}

	public void start() {
		// 创建线程池
		executorService = Executors.newFixedThreadPool(5);
	}

	public boolean isStop() {
		return executorService.isShutdown();
	}
	class DownloadThread implements Runnable {
	        private String url;
	        private String filename;
	        private String path;
	        private UpdateVo uv;

		public DownloadThread(DownloadItem item) {
		  
			url=r.getUrl("/"+item.getUrl());
			
			filename=item.getFileName();
			path=item.getPath();
			uv=item.getUv();
	
		}

		@Override
		public void run() {
		    HttpClient httpClient = new DefaultHttpClient();
		    System.out.println("-url----------"+url);
		    HttpGet httpGet = new HttpGet(url);
		    HttpResponse httpResponse;
		    try {
			
			    httpResponse = httpClient.execute(httpGet);
			
			HttpEntity httpEntity = httpResponse.getEntity();
			
			Bitmap bitmap = BitmapFactory.decodeStream(httpEntity.getContent());
			   boolean b=false;
			if(bitmap!=null){
			    HttpDownload httpdownload=new HttpDownload();
		            b= httpdownload.saveFile(bitmap, filename, path);
			}			
			
			if(b){			
			 
			    db.update(uv.getTablename(), uv.getValues(),uv.getWhereClause(), null);
			      Message msg = downhandler.obtainMessage();
				msg.what = 0;	                        
				downhandler.sendMessage(msg);				
			}else{			   
			       ContentValues value=uv.getValues();
				value.put("isdownload", "2");
	                        db.update(uv.getTablename(), value,uv.getWhereClause(), null);			  
			        Message msg = downhandler.obtainMessage();
				msg.what = 2;				
				downhandler.sendMessage(msg);
			}			
	
		    } catch (Exception e) {		
			   Message msg = downhandler.obtainMessage();
				msg.what = 2;				
				downhandler.sendMessage(msg);
			    e.printStackTrace();
			} finally{
			httpClient.getConnectionManager().shutdown();	
		    }

		}
	}
}
